home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / os2 / pccts.zip / ERR.H < prev    next >
C/C++ Source or Header  |  1992-12-08  |  5KB  |  220 lines

  1. /*
  2.  * err.h
  3.  *
  4.  * Standard error handling mechanism
  5.  *
  6.  * Terence Parr, Hank Dietz and Will Cohen: 1989, 1990, 1991, 1992
  7.  * Purdue University Electrical Engineering
  8.  * ANTLR Version 1.06
  9.  */
  10.  
  11. #include <string.h>
  12. #ifdef __STDC__
  13. #include <stdarg.h>
  14. #else
  15. #include <varargs.h>
  16. #endif
  17.  
  18. /* Define usable bits per unsigned int word (used for set stuff) */
  19. #ifdef PC
  20. #define WORDSIZE 16
  21. #define LogWordSize    4
  22. #else
  23. #define    WORDSIZE 32
  24. #define LogWordSize 5
  25. #endif
  26.  
  27. #define    MODWORD(x) ((x) & (WORDSIZE-1))        /* x % WORDSIZE */
  28. #define    DIVWORD(x) ((x) >> LogWordSize)        /* x / WORDSIZE */
  29.  
  30. /* maximum of 32 bits/unsigned int and must be 8 bits/byte */
  31. static unsigned bitmask[] = {
  32.     0x00000001, 0x00000002, 0x00000004, 0x00000008,
  33.     0x00000010, 0x00000020, 0x00000040, 0x00000080,
  34.     0x00000100, 0x00000200, 0x00000400, 0x00000800,
  35.     0x00001000, 0x00002000, 0x00004000, 0x00008000,
  36.     0x00010000, 0x00020000, 0x00040000, 0x00080000,
  37.     0x00100000, 0x00200000, 0x00400000, 0x00800000,
  38.     0x01000000, 0x02000000, 0x04000000, 0x08000000,
  39.     0x10000000, 0x20000000, 0x40000000, 0x80000000
  40. };
  41.  
  42. void
  43. #ifdef __STDC__
  44. zzresynch(unsigned *wd,unsigned mask)
  45. #else
  46. zzresynch(wd,mask)
  47. unsigned *wd, mask;
  48. #endif
  49. {
  50.     while ( !(wd[LA(1)]&mask) && LA(1) != zzEOF_TOKEN ) {zzCONSUME;}
  51. }
  52.  
  53. /* input looks like:
  54.  *        zzFAIL(k, e1, e2, ...,&zzMissSet,&zzMissText,&zzBadTok,&zzBadText)
  55.  * where the zzMiss stuff is set here to the token that did not match
  56.  * (and which set wasn't it a member of).
  57.  */
  58. void
  59. #ifdef __STDC__
  60. zzFAIL(int k, ...)
  61. #else
  62. zzFAIL(va_alist)
  63. va_dcl
  64. #endif
  65. {
  66. #ifdef LL_K
  67.     static char text[LL_K*ZZLEXBUFSIZE+1];
  68.     unsigned *f[LL_K];
  69. #else
  70.     static char text[ZZLEXBUFSIZE+1];
  71.     unsigned *f[1];
  72. #endif
  73.     unsigned **miss_set;
  74.     char **miss_text;
  75.     unsigned *bad_tok;
  76.     char **bad_text;
  77.     unsigned *err_k;
  78.     int i;
  79.     va_list ap;
  80. #ifndef __STDC__
  81.     int k;
  82. #endif
  83. #ifdef __STDC__
  84.     va_start(ap, k);
  85. #else
  86.     va_start(ap);
  87.     k = va_arg(ap, int);    /* how many lookahead sets? */
  88. #endif
  89.     text[0] = '\0';
  90.     for (i=1; i<=k; i++)    /* collect all lookahead sets */
  91.     {
  92.         f[i-1] = va_arg(ap, unsigned *);
  93.     }
  94.     for (i=1; i<=k; i++)    /* look for offending token */
  95.     {
  96.         if ( i>1 ) strcat(text, " ");
  97.         strcat(text, LATEXT(i));
  98.         if ( !zzset_el(LA(i), f[i-1]) ) break;
  99.     }
  100.     miss_set = va_arg(ap, unsigned **);
  101.     miss_text = va_arg(ap, char **);
  102.     bad_tok = va_arg(ap, unsigned *);
  103.     bad_text = va_arg(ap, char **);
  104.     err_k = va_arg(ap, unsigned *);
  105.     if ( i>k )
  106.     {
  107.         /* bad; lookahead is permutation that cannot be matched,
  108.          * but, the ith token of lookahead is valid at the ith position
  109.          * (The old LL sub 1 (k) versus LL(k) parsing technique)
  110.          */
  111.         *miss_set = NULL;
  112.         *miss_text = zzlextext;
  113.         *bad_tok = LA(1);
  114.         *bad_text = LATEXT(1);
  115.         *err_k = k;
  116.         return;
  117.     }
  118. /*    fprintf(stderr, "%s not in %dth set\n", zztokens[LA(i)], i);*/
  119.     *miss_set = f[i-1];
  120.     *miss_text = text;
  121.     *bad_tok = LA(i);
  122.     *bad_text = LATEXT(i);
  123.     if ( i==1 ) *err_k = 1;
  124.     else *err_k = k;
  125. }
  126.  
  127. /* standard error reporting function */
  128. void
  129. #ifdef __STDC__
  130. zzsyn(char *text, unsigned tok, char *egroup, unsigned *eset, int etok, int k, char *bad_text)
  131. #else
  132. zzsyn(text, tok, egroup, eset, etok, k, bad_text)
  133. char *text, *egroup, *bad_text;
  134. unsigned tok;
  135. int etok, k;
  136. unsigned *eset;
  137. #endif
  138. {
  139.     
  140.     fprintf(stderr, "line %d: syntax error at \"%s\"", zzline, (tok==zzEOF_TOKEN)?"EOF":text);
  141.     if ( !etok && !eset ) {fprintf(stderr, "\n"); return;}
  142.     if ( k==1 ) fprintf(stderr, " missing");
  143.     else
  144.     {
  145.         fprintf(stderr, "; \"%s\" not", bad_text);
  146.         if ( zzset_deg(eset)>1 ) fprintf(stderr, " in");
  147.     }
  148.     if ( zzset_deg(eset)>0 ) zzedecode(eset);
  149.     else fprintf(stderr, " %s", zztokens[etok]);
  150.     if ( strlen(egroup) > 0 ) fprintf(stderr, " in %s", egroup);
  151.     fprintf(stderr, "\n");
  152. }
  153.  
  154. /* is b an element of set p? */
  155. int
  156. #ifdef __STDC__
  157. zzset_el(unsigned b, unsigned *p)
  158. #else
  159. zzset_el(b,p)
  160. unsigned b;
  161. unsigned *p;
  162. #endif
  163. {
  164.     return( p[DIVWORD(b)] & bitmask[MODWORD(b)] );
  165. }
  166.  
  167. int
  168. #ifdef __STDC__
  169. zzset_deg(unsigned *a)
  170. #else
  171. zzset_deg(a)
  172. unsigned *a;
  173. #endif
  174. {
  175.     /* Fast compute degree of a set... the number
  176.        of elements present in the set.  Assumes
  177.        that all word bits are used in the set
  178.     */
  179.     register unsigned *p = a;
  180.     register unsigned *endp = &(a[zzSET_SIZE]);
  181.     register unsigned degree = 0;
  182.  
  183.     if ( a == NULL ) return 0;
  184.     while ( p < endp )
  185.     {
  186.         register unsigned t = *p;
  187.         register unsigned *b = &(bitmask[0]);
  188.         do {
  189.             if (t & *b) ++degree;
  190.         } while (++b < &(bitmask[WORDSIZE]));
  191.         p++;
  192.     }
  193.  
  194.     return(degree);
  195. }
  196.  
  197. void
  198. #ifdef __STDC__
  199. zzedecode(unsigned *a)
  200. #else
  201. zzedecode(a)
  202. unsigned *a;
  203. #endif
  204. {
  205.     register unsigned *p = a;
  206.     register unsigned *endp = &(p[zzSET_SIZE]);
  207.     register unsigned e = 0;
  208.  
  209.     if ( zzset_deg(a)>1 ) fprintf(stderr, " {");
  210.     do {
  211.         register unsigned t = *p;
  212.         register unsigned *b = &(bitmask[0]);
  213.         do {
  214.             if ( t & *b ) fprintf(stderr, " %s", zztokens[e]);
  215.             e++;
  216.         } while (++b < &(bitmask[sizeof(unsigned)*8]));
  217.     } while (++p < endp);
  218.     if ( zzset_deg(a)>1 ) fprintf(stderr, " }");
  219. }
  220.